package xju_5_6.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xju_5_6.entity.User;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/test")
public class UserController {

//    @RequestMapping(method = RequestMethod.GET,value = "/**")
    @GetMapping("/te")
    @ResponseBody
    public String test(@RequestParam("name") String username,@RequestParam(value = "password",required = false,defaultValue = "321") String password) {
        System.out.println("test");
        System.out.println(username);
        System.out.println(password);
        return username+":"+password;
    }

    @RequestMapping("/t")
    public String test1() {
        System.out.println("test1");
        return "hello";
    }

    // 一名多值
    @RequestMapping("/data")
    public String test2(@RequestParam("num") List<String> num) {
        System.out.println(num);
        return "hello";
    }
    // 实体参数
    @RequestMapping("/data1")
    public String test3(User user) {
        System.out.println(user);
        return "hello";
    }

    // 路径传参
    @RequestMapping("/delete/{id}/{name1}")
    public String test4(@PathVariable("id") Integer id,@PathVariable("name1") String name) {
        System.out.println(id+":"+name);
        return "hello";
    }

    // json传参
    @RequestMapping("/json")
    public String test5(@RequestBody User user) {
        System.out.println(user);
        return "hello";
    }

    // cookie
    @RequestMapping("/cookie")
    public String test6(@CookieValue("JSESSIONID")String cookie) {
        System.out.println(cookie);
        return "hello";
    }

    // header
    @RequestMapping("/requestHeader")
    public String test7(@RequestHeader("host") String host,@RequestHeader("Connection") String header,@RequestHeader("test")String test) {
        System.out.println(test);
        System.out.println(host);
        System.out.println(header);
        return "hello";
    }

    // request 级别共享域
    @RequestMapping("/request")
    public String test8(Model model, ModelMap modelMap, Map<String,String> map, HttpServletRequest request) {
        model.addAttribute("model", "modelData");
        modelMap.addAttribute("modelMap", "modelMapData");
        map.put("map", "mapData");
        request.setAttribute("request", "requestData");
        return "hello";
    }
    @RequestMapping("/modelAndView")
    public ModelAndView test9() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        modelAndView.addObject("modelAndView", "modelAndViewData");
        return modelAndView;
    }

    // session
    @RequestMapping("/session")
    public String test10(HttpSession session) {
        session.setAttribute("session", "sessionData");
        return "hello";
    }
//servletContext
    @Autowired
    private ServletContext servletContext;
    @RequestMapping("servletContext")
    public String test11() {
        servletContext.setAttribute("servletContext", "servletContextData");
        return "hello";
    }

    // 转发 forward
    @RequestMapping("/forward")
    public String test12() {
        return "forward:/test/modelAndView";
    }

    // 重定向 redirect
    @RequestMapping("/redirect")
    public String test13() {
        return "redirect:/test/request";
    }

    // 访问登陆页面
    @RequestMapping("/toLogin")
    public String test14(HttpSession session) {
        return "login";
    }
}
